//
//  GrowRecordViewController.m
//  PigMarket
//
//  Created by mac on 2019/7/3.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "GrowRecordViewController.h"
#import "GrowRecordCell.h"
#import "GrowRecordReusableView.h"
#import "GrowRecordDetailViewController.h"

@interface GrowRecordViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**
 flowLayout
 */
@property (nonatomic , strong) UICollectionViewFlowLayout *flowLayout;
/**
 myCollectionView
 */
@property (nonatomic , strong) UICollectionView *myCollectionView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
@end

@implementation GrowRecordViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"生长报告";
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myCollectionView];
}

- (void) initLayout {
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] growRecordWithPigId:self.pigId] subscribeNext:^(GrowRecordAllList * _Nullable growRecordList) {
        @strongify(self);
        [self.dataArr addObjectsFromArray:growRecordList.list];
        [self.myCollectionView reloadData];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate

#pragma mark - target action

#pragma mark - private method
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataArr.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    GrowRecordList *model = self.dataArr[section];
    return [model.list count];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    GrowRecordList *model = self.dataArr[indexPath.section];

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        GrowRecordReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
        header.backgroundColor = [UIColor globalBackgroundColor];
        header.dateLab.text = model.month;
        return header;
    }
    return nil;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GrowRecordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GrowRecordCell" forIndexPath:indexPath];
    GrowRecordList *model = self.dataArr[indexPath.section];
    
    cell.model = model.list[indexPath.item];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GrowRecordList *model = self.dataArr[indexPath.section];
    GrowRecord *pigGrowReportIdModel = model.list[indexPath.item];
    GrowRecordDetailViewController *vc = [[GrowRecordDetailViewController alloc] init];
    vc.pigGrowReportId = pigGrowReportIdModel.pigGrowReportId;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - getter and setter
- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //每个item的大小
        _flowLayout.itemSize = CGSizeMake(ScaleWidth(166), ScaleWidth(141));
        //行间距
        _flowLayout.minimumLineSpacing = ScaleWidth(10);
        //每行item之间的间距
        _flowLayout.minimumInteritemSpacing = ScaleWidth(10);
        //每个tiem距离上下左右的距离
        _flowLayout.sectionInset = UIEdgeInsetsMake(15, 13, 13, 15);
        
        _flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 34);
    }
    return _flowLayout;
}
- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        [_myCollectionView registerClass:[GrowRecordCell class] forCellWithReuseIdentifier:@"GrowRecordCell"];
        [_myCollectionView registerClass:[GrowRecordReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
        _myCollectionView.backgroundColor = [UIColor whiteColor];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
    }
    return _myCollectionView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

@end
